﻿Public Class _Default
    Inherits System.Web.UI.Page
    Private Function GetNextDate()
        'Dim now As DateTime = DateTime.Today
        'Dim today As Integer = CInt(now.DayOfWeek)
        'Dim find As Integer = CInt(day)
        'Dim delta As Integer = find - today
        'If delta > 0 Then
        '    Return now.AddDays(delta)
        'Else
        '    Return now.AddDays(7 - delta)
        'End If
        Dim days As New NameValueCollection()
        Dim now As DateTime = DateTime.Today
        Dim today As Integer = CInt(now.DayOfWeek)
        Select Case today
            Case 0
                Dim nextDay As Integer() = {1, 2, 3, 4, 5, 6}
                ' Dim days As New ArrayList()
                days = GetNextDates(nextDay, now.DayOfWeek)
                'GetPreviousDates(1, now.DayOfWeek)
            Case 1
                Dim nextDay As Integer() = {0, 1, 2, 3, 4, 5, 6}
                days = GetNextDates(nextDay, now.DayOfWeek)
            Case 2


            Case 3

            Case 4
                Dim nextDay As Integer() = {0, 1, 2, 3, 4, 5, 6}
                days = GetNextDates(nextDay, now.DayOfWeek)
            Case 5

            Case 6
                Dim nextDay As Integer() = {0, 1, 2, 3, 4, 5, 6}
                days = GetNextDates(nextDay, now.DayOfWeek)
        End Select
        Return days
    End Function
    Private Function GetPreviousDates(ByVal nDays As Integer(), ByVal today As System.DayOfWeek) As ArrayList
        Dim days As New ArrayList()
        For i = 0 To nDays.Length - 1
            Dim find As Integer = nDays(i)
            Dim delta As Integer = find - today
            If delta > 0 Then
                days.Add(Now.AddDays(delta))
            Else
                days.Add(Now.AddDays(7 - delta))
            End If
        Next
        Return days
    End Function
    Private Function GetNextDates(ByVal nDays As Integer(), ByVal today As System.DayOfWeek) As NameValueCollection
        Dim days As New Hashtable()
        Dim li As New NameValueCollection()
        For i = 0 To nDays.Length - 1
            Dim find As Integer = nDays(i)
            Dim delta As Integer = find - today
            Dim da As System.DateTime
            da = Now.AddDays(delta)
            li.Add(Now.AddDays(delta).DayOfWeek.ToString(), Now.AddDays(delta))
        Next




        'For i = 0 To nDays.Length - 1
        '    Dim find As Integer = nDays(i)
        '    Dim delta As Integer = find - today
        '    Dim da As System.DateTime
        '    If delta > 0 Then
        '        da = Now.AddDays(delta)
        '        days.Add(Now.AddDays(nDays(i)).DayOfWeek.ToString(), Now.AddDays(delta))
        '    Else
        '        days.Add(Now.AddDays(nDays(i)).DayOfWeek.ToString, Now.AddDays(7 - delta))
        '    End If
        'Next
        Return li
    End Function
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim days As New NameValueCollection()
        days = GetNextDate()
        Dim dd As System.DayOfWeek
        '   Dim na As String = dd(0).tostring()

        'Dim dates = GetNextDate(DayOfWeek.Saturday)
        'Dim today As Date = Date.Today
        'Dim dayIndex As Integer = today.DayOfWeek
        'If dayIndex < DayOfWeek.Monday ThenS
        '    dayIndex += 7 'Monday is first day of week, no day of week should have a smaller index
        'End If
        'Dim dayDiff As Integer = dayIndex - DayOfWeek.Monday
        'Dim monday As Date = today.AddDays(-dayDiff)



        'Dim dates As Date
        'dates = System.DateTime.Now
        'Dim dWeek As System.DayOfWeek
        'dWeek = dates.DayOfWeek
        'Select Case dWeek.ToString()
        '    Case "Monday"

        '    Case "Tuesday"

        '    Case "Wednesday"

        '    Case "Thursday"

        '    Case "Friday"

        '    Case "Saturday"

        '    Case "Sunday"

        'End Select
        Dim dt As New DataTable()
        For i = 0 To days.Count - 1
            Dim dat As System.DateTime
            Dim a As String()
            a = days.GetValues(i)
            dat = System.DateTime.Parse(a(0))
            Dim s As String = dat.ToString("MM-dd")
            dt.Columns.Add(days.GetKey(i) & "<br/>" & s)
        Next
        For Each Va In days



            '  
        Next

        For i = 0 To 12
            dt.Rows.Add(dt.NewRow())
        Next i
        'dt.Columns.Add("col1")
        'dt.Columns.Add("col2")
        'Dim dr = dt.NewRow()
        'dr("col1") = "asdfasdfasdf"
        'dr("col2") = "asdfasdf"
        'dt.Rows.Add(dr)

        GridView1.DataSource = dt
        GridView1.DataBind()
        GridView1.HeaderRow.Cells(1).Text = dt.Columns(0).ToString()
        GridView1.HeaderRow.Cells(2).Text = dt.Columns(1).ToString()
        GridView1.HeaderRow.Cells(3).Text = dt.Columns(2).ToString()
        GridView1.HeaderRow.Cells(4).Text = dt.Columns(3).ToString()
        GridView1.HeaderRow.Cells(5).Text = dt.Columns(4).ToString()
        GridView1.HeaderRow.Cells(6).Text = dt.Columns(5).ToString()
        GridView1.HeaderRow.Cells(7).Text = dt.Columns(6).ToString()
        Dim aa As Label
        aa = GridView1.Rows(0).FindControl("lblTotal")
        ' aa.Text = 40


    End Sub
    Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            'Dim txtMon As TextBox = e.Row.FindControl("txtMon")
            'txtMon.Attributes.Add("onBlur", "JvfunonBlur();")
            Dim txtWed As TextBox
            Dim txtMon As TextBox
            Dim txtTue As TextBox
            Dim txtSat As TextBox
            Dim txtSun As TextBox
            Dim txtThur As TextBox
            Dim txtFri As TextBox
            Dim total As TextBox
            total = e.Row.FindControl("txtTotal")
            txtMon = e.Row.FindControl("txtMon")
            txtWed = e.Row.FindControl("txtWed")
            txtTue = e.Row.FindControl("txtTue")
            txtThur = e.Row.FindControl("txtThur")
            txtFri = e.Row.FindControl("txtFri")
            txtSat = e.Row.FindControl("txtSat")
            txtSun = e.Row.FindControl("txtSun")
            Dim all As TextBox
            all = e.Row.FindControl("txtTotalAll")
            txtMon.Attributes.Add("onBlur", "JvfunonBlur('" + txtMon.ClientID + "','" + txtTue.ClientID + "', '" + txtWed.ClientID + "', '" + txtThur.ClientID + "', '" + txtFri.ClientID + "', '" + txtSat.ClientID + "', '" + txtSun.ClientID + "' , '" + total.ClientID + "');")
            txtTue.Attributes.Add("onBlur", "JvfunonBlur('" + txtMon.ClientID + "','" + txtTue.ClientID + "', '" + txtWed.ClientID + "', '" + txtThur.ClientID + "', '" + txtFri.ClientID + "', '" + txtSat.ClientID + "', '" + txtSun.ClientID + "', '" + total.ClientID + "' );")
            txtWed.Attributes.Add("onBlur", "JvfunonBlur('" + txtMon.ClientID + "', '" + txtTue.ClientID + "', '" + txtWed.ClientID + "', '" + txtThur.ClientID + "', '" + txtFri.ClientID + "', '" + txtSat.ClientID + "', '" + txtSun.ClientID + "', '" + total.ClientID + "' );")
            txtThur.Attributes.Add("onBlur", "JvfunonBlur('" + txtMon.ClientID + "','" + txtTue.ClientID + "', '" + txtWed.ClientID + "', '" + txtThur.ClientID + "', '" + txtFri.ClientID + "', '" + txtSat.ClientID + "', '" + txtSun.ClientID + "', '" + total.ClientID + "' );")
            txtFri.Attributes.Add("onBlur", "JvfunonBlur('" + txtMon.ClientID + "','" + txtTue.ClientID + "', '" + txtWed.ClientID + "', '" + txtThur.ClientID + "', '" + txtFri.ClientID + "', '" + txtSat.ClientID + "', '" + txtSun.ClientID + "', '" + total.ClientID + "' );")
            txtSat.Attributes.Add("onBlur", "JvfunonBlur('" + txtMon.ClientID + "','" + txtTue.ClientID + "', '" + txtWed.ClientID + "', '" + txtThur.ClientID + "', '" + txtFri.ClientID + "', '" + txtSat.ClientID + "', '" + txtSun.ClientID + "', '" + total.ClientID + "' );")
            txtSun.Attributes.Add("onBlur", "JvfunonBlur('" + txtMon.ClientID + "','" + txtTue.ClientID + "', '" + txtWed.ClientID + "', '" + txtThur.ClientID + "', '" + txtFri.ClientID + "', '" + txtSat.ClientID + "', '" + txtSun.ClientID + "' , '" + total.ClientID + "');")
            total.Attributes.Add("onBlur", "JvfunonBlur('" + txtMon.ClientID + "','" + txtTue.ClientID + "', '" + txtWed.ClientID + "', '" + txtThur.ClientID + "', '" + txtFri.ClientID + "', '" + txtSat.ClientID + "', '" + txtSun.ClientID + "' , '" + total.ClientID + "');")

        End If
    End Sub

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        'string attachment = "attachment; filename=Emp.xls";
        '    Response.ClearContent();
        '    Response.AddHeader("content-disposition", attachment);
        '    Response.ContentType = "application/ms-excel";
        '    StringWriter sw = new StringWriter();
        '    HtmlTextWriter htw = new HtmlTextWriter(sw);
        '    GridVwExportExcel.RenderControl(htw);
        '    Response.Write(sw.ToString());
        '    Response.End();
        'Dim attachment As String = "attachment; filename=Emp.xls"
        'Response.ClearContent()
        'Response.AddHeader("content-disposition", attachment)
        'Response.ContentType = "application/ms-excel"
        'Dim sw As New IO.StringWriter
        'Dim htw As New HtmlTextWriter(sw)
        'GridView1.RenderControl(htw)
        'Response.Write(sw.ToString())
        'Response.End()



        'Response.Clear();
        'Response.AddHeader("content-disposition", "attachment;filename=file-name.xls");
        'Response.ContentType = "application/vnd.xlsx";
        'System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        'System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        'GridView1.RenderControl(htmlWrite);
        'Response.Write(stringWrite.ToString());
        'Response.End();
        Response.Clear()
        Response.AddHeader("content-disposition", "attachment;filename=test.xls")
        Response.ContentType = "application/vnd.xlsx"
        Dim sw As New IO.StringWriter
        Dim hmlW As New HtmlTextWriter(sw)
        GridView1.RenderControl(hmlW)
        Response.Write(sw.ToString())
        Response.End()

    End Sub
End Class